<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $nombres
 * @property string $apellido_paterno
 * @property string $apellido_materno
 * @property string $run
 * @property string $username
 * @property string $password
 * @property integer $activo
 * @property string $email
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public $foto;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombres, apellido_paterno, apellido_materno, run, username, password, email, activo, direccion, telefono','required'),
			array('activo, telefono', 'numerical', 'integerOnly'=>true),
			array('foto','file', 'types'=>'jpg, gif, png','allowEmpty' => true),
			array('run, username','unique'),
			array('run','length','min'=>11,'max'=>12),
			array('telefono','length','min'=>6,'max'=>12),
			array('email','email'),
			array('creado','default','value'=>new CDbExpression('NOW()'),'setOnEmpty'=>false,'on'=>'insert'),
			array('nombres, apellido_paterno, apellido_materno, run, username, password, email, direccion, salud, prevision,foto_src', 'length', 'max'=>45,'min'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('foto,id, nombres, apellido_paterno, apellido_materno, run, username, password, activo, email', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'administradors' => array(self::HAS_MANY, 'Administrador', 'user_id'),
		'administrativos' => array(self::HAS_MANY, 'Administrativo', 'user_id'),
		'profesors' => array(self::HAS_MANY, 'Profesor', 'user_id'),
		'profesor' => array(self::HAS_ONE, 'Profesor', 'user_id'),
		'alumno' => array(self::HAS_ONE, 'Alumno', 'user_id'),
		'alumnos' => array(self::HAS_MANY, 'Alumno', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID Usuario',
			'nombres' => 'Nombres',
			'apellido_paterno' => 'Apellido Paterno',
			'apellido_materno' => 'Apellido Materno',
			'run' => 'RUN',
			'username' => 'Nombre de Usuario',
			'password' => 'Contraseña',
			'activo' => 'Activo',
			'email' => 'Email',
			'creado' => 'Creado',
			'direccion' => 'Direccion',
			'telefono' => 'Telefono',
			'salud' => 'Previsión de salud',
			'prevision' => 'Previsión AFP',
			'foto' => 'Imagen',
			'foto_src'=> 'Imagen'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->condition='activo=1';
		$criteria->compare('id',$this->id);
		$criteria->compare('nombres',$this->nombres,true);
		$criteria->compare('apellido_paterno',$this->apellido_paterno,true);
		$criteria->compare('apellido_materno',$this->apellido_materno,true);
		$criteria->compare('run',$this->run,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('activo',$this->activo);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('telefono',$this->telefono);
		$criteria->compare('salud',$this->salud,true);
		
		$sort = new CSort();
		
		$sort-> attributes = array(
							'nombres'=>array(
								'asc'=>'nombres',
								'desc'=>'nombres DESC',
		),
							'apellido_paterno'=>array(
								'asc'=>'apellido_paterno',
								'desc'=>'apellido_paterno DESC',
		),
							'apellido_materno'=>array(
								'asc'=>'user.apellido_materno',
								'desc'=>'user.apellido_materno DESC',
		),
							'run'=>array(
								'asc'=>'run',
								'desc'=>'run DESC',
		),
								'username'=>array(
									'asc'=>'username',
									'desc'=>'username DESC',
		),
							'email'=>array(
								'asc'=>'email',
								'desc'=>'email DESC',
		),
					'*',
		);
		
		$_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>$sort,
				'pagination'=>false,
		));

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria, 'sort'=> $sort, 'pagination'=>array(
		'pageSize'=>10,),
		));
	}
	
	public function buscarDesactivados(){
		$criteria=new CDbCriteria;
		$criteria->condition='activo=0';
		$criteria->compare('id',$this->id);
		$criteria->compare('nombres',$this->nombres,true);
		$criteria->compare('apellido_paterno',$this->apellido_paterno,true);
		$criteria->compare('apellido_materno',$this->apellido_materno,true);
		$criteria->compare('run',$this->run,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('activo',$this->activo);
		$criteria->compare('email',$this->email,true);
		
		return new CActiveDataProvider($this, array(
					'criteria'=>$criteria,
		));
	}
	
	public function validatePassword($password)
	{
		return $this->hashPassword($password)===$this->password;
	}
	public function hashPassword($password)
	{
		return md5($password);
	}
	
	public function getNombreCompleto(){
		return $this->nombres.' '.$this->apellido_paterno.' '.$this->apellido_materno;
	}
	
	public function scopes(){
		return array(
			'activo'=>array(
				'condition'=>'activo=1',
			),
			'inactivo'=>array(
				'condition'=>'activo=0',
			),
		
		);
	}
	/*
	public function beforeSave()
	{
		if(parent::beforeSave())
		{
			if(isset($this->foto)){
				$id=$this->dbConnection->createCommand('SELECT MAX(id) FROM user')->queryScalar();//obtenemos el maximo ID (el acutal)
				$id=$id+1;//le agregamos uno ya que al guardar se guardara con uno mas
				$foto_src='/images/user'.'/'.$id.'/'.$this->username.'.'.$this->foto->extensionName; //Guardamos el path con la id del usuario y el nombre de usuario
				$this->setAttribute('foto_src', $foto_src);
			}
			
		}
		return true;
	}*/
	
	/*public function afterSave(){
		if(parent::afterSave()){
			if(isset($this->foto)){
				$foto_src='/images/user'.'/'.$this->id.'/'.$this->username.'.'.$this->foto->extensionName;
				$this->setAttribute('foto_src', $foto_src);
			}
		}
	}*/
	
}